// pages/label/addLabel/addLabe.js
import {
    baseUrl,
    isHaveToken
} from "../../../utils/util";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        textarea: "",
        originFiles: [],
        FilesLight: '0',
        imageUploaded: [],
        label_id: "",
        index: 0,
        radio:"树洞",// 单选按钮
        disabled: false,
        finish: false,
        loading: false,
        gridConfig: {
            column: 3,
            width: 200,
            height: 200,
        },
        config: {
            count: 1,
        },
        disabled: false,
    },
    handleSuccess(e) {
        const {
            files
        } = e.detail;
        this.setData({
            originFiles: files,
        });
        // console.log("选中了图片")
        this.uploadImage(this.data.originFiles.length)
        this.setData({
            loading: false
        });
    },
    onChange(event) {
        console.log('radio', event.detail);
        this.setData({
            radio:event.detail.value
        })
      },
    handleRemove(e) {
        const {
            index
        } = e.detail;
        const {
            originFiles
        } = this.data;
        originFiles.splice(index, 1);
        this.setData({
            originFiles
        });
    },
    textChange(e) {
        // console.log("textChange",e),
        this.setData({
            textarea: e.detail.value
        })
    },
        getUserInfo() {
        var that = this
        wx.request({
            header: {
                token: wx.getStorageSync('token')
            },
            url: `${baseUrl}/ucenter/member/getMemberInfo`,
            success(res) {
                console.log("userInfo", res)
                that.setData({
                        userInfo: res.data.data.userInfo
                    }),
                    wx.setStorageSync('userInfo', res.data.data.userInfo)
            }
        })
    },
    // 上传按钮
    upload(e) {
        // console.log(this.data.textarea)
        // console.log(this.data.originFiles)
        // console.log("originFiles lenght", this.data.originFiles.length)
        console.log("this.data.radio",this.data.radio)
        if (this.data.textarea == "") {
            // console.log("内容为空")
            wx.showToast({
              title: '内容为空',
              icon:'error',
              duration:1000
            })
        } else {
            this.uploadFile()
            // console.log(this.data.imageUploaded)
            this.setData({
                textarea: "",
                originFiles: [],
                loading:false
            })
        }
    },
    // 上传网络
    uploadFile() {
        var userInfo = wx.getStorageSync('userInfo')
        var that = this
        wx.request({
            url: `${baseUrl}/label/add`,
            method: 'POST',
            data: {
                "comment": 0,
                "content": this.data.textarea,
                "likes": 0,
                "state": "我超叼的",
                "type": this.data.radio,
                "userId": userInfo.id
            },
            success(res) {
                // console.log("uploadFile res", res)
                if (res.data.code == 20000) {
                    that.setData({
                        label_id: res.data.data.label_id,
                        loading: false
                    })
                    if (that.data.finish) {
                        that.uploadImageList()
                        that.setData({
                            loading: false
                        })
                    }
                    wx.showToast({
                        title: '发布成功',
                        duration: 1000,
                        icon: "success"
                    })
                } else {
                    wx.showToast({
                        title: '发布失败',
                        duration: 500,
                        icon: 'error'
                    })
                }
            }
        })
    },

    //上传图片
    uploadImage(FilesLight) {
        var image = []
        var that = this
        that.setData({
            loading: true,
            disabled: true
        })
        for (var i = 0; i < FilesLight; i++) {
            console.log("originFilex", this.data.originFiles[i].url)
            wx.uploadFile({
                filePath: this.data.originFiles[i].url,
                name: 'file',
                url: `${baseUrl}/oss/label`,
                success(res) {
                    var data = JSON.parse(res.data)
                    // console.log(data)
                    // console.log("data.data", data.data)
                    image.push(data.data.url)
                    // console.log("data.image", image)
                    // console.log("image ", image.length)
                    if (image.length == FilesLight) {
                        console.log("我上传完啦")
                        that.setData({
                            finish: true,
                            loading:false,
                            disabled: false
                        })
                    }
                }
            })
        }
        that.setData({
            imageUploaded: image,
        })

        // wx.request({
        //   url: `${baseUrl}/oss/label`,
        //   files:this.data.originFiles[0],
        //   success(res){
        //       console.log(res)
        //   }
        // })
    },

    // 上传网络
    uploadImageList() {
        // console.log("uploadImageList", this.data.imageUploaded)
        // console.log("labelId", this.data.label_id)
        // var userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: `${baseUrl}/label/img/addImgList`,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                imgList: this.data.imageUploaded,
                labelId: this.data.label_id
            },
            success(res) {
                // console.log("uploadImageList", res)
                // if (res.data.code == 20000) {
                //     wx.showToast({
                //         title: '发布成功',
                //         duration: 500,
                //         icon: "success"
                //     })
                // } else {
                //     wx.showToast({
                //         title: '发布失败',
                //         duration: 500,
                //         icon: 'error'
                //     })
                // }
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})